package com.example.lyl.yuejian.view;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.view.fragment.ClubFragment;
import com.example.lyl.yuejian.view.fragment.HomeFragment;
import com.example.lyl.yuejian.view.fragment.MyFragment;
import com.example.lyl.yuejian.view.fragment.PhotoFragment;
import com.example.lyl.yuejian.view.fragment.ShopFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends AppCompatActivity {

    @BindView(R.id.replaceView)
    FrameLayout replaceView;
    @BindView(R.id.shopRb)
    RadioButton shopRb;
    @BindView(R.id.clubRb)
    RadioButton clubRb;
    @BindView(R.id.homeRb)
    RadioButton homeRb;
    @BindView(R.id.photoRb)
    RadioButton photoRb;
    @BindView(R.id.myRb)
    RadioButton myRb;
    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;


    private HomeFragment homeFragment;
    private ClubFragment clubFragment;
    private PhotoFragment photoFragment;
    private ShopFragment shopFragment;
    private MyFragment myFragment;

    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);

        ButterKnife.bind(this);


        initFragment();

        changeImageSize();

        manager = getSupportFragmentManager();

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = manager.beginTransaction();
                switch (checkedId) {
                    case R.id.shopRb:
                        transaction.replace(R.id.replaceView, shopFragment);
                        break;
                    case R.id.clubRb:
                        transaction.replace(R.id.replaceView, clubFragment);
                        break;
                    case R.id.homeRb:
                        transaction.replace(R.id.replaceView, homeFragment);
                        break;
                    case R.id.photoRb:
                        transaction.replace(R.id.replaceView, photoFragment);
                        break;
                    case R.id.myRb:
                        transaction.replace(R.id.replaceView, myFragment);
                        break;
                }
                transaction.commit();
            }
        });

        radioGroup.check(R.id.homeRb);
    }

    private void initFragment() {
        homeFragment = new HomeFragment();
        clubFragment = new ClubFragment();
        myFragment = new MyFragment();
        photoFragment = new PhotoFragment();
        shopFragment = new ShopFragment();
    }


    private void changeImageSize() {
        Drawable drawableFirst = getResources().getDrawable(R.drawable.homeicon_select);
        drawableFirst.setBounds(0, 10, 100, 100);
        shopRb.setCompoundDrawables(null, drawableFirst, null, null);

        Drawable drawableSearch = getResources().getDrawable(R.drawable.jvlebu_selector);
        drawableSearch.setBounds(0, 10, 100, 100);
        clubRb.setCompoundDrawables(null, drawableSearch, null, null);

        Drawable drawableMe = getResources().getDrawable(R.drawable.zhuye_selector);
        drawableMe.setBounds(0, 10, 100, 100);
        homeRb.setCompoundDrawables(null, drawableMe, null, null);

        Drawable drawableShaitu = getResources().getDrawable(R.drawable.shaitu_selector);
        drawableShaitu.setBounds(0, 10, 100, 100);
        photoRb.setCompoundDrawables(null, drawableShaitu, null, null);

        Drawable drawablewo = getResources().getDrawable(R.drawable.wo_selector);
        drawablewo.setBounds(0, 10, 100, 100);
        myRb.setCompoundDrawables(null, drawablewo, null, null);
    }

}
